package Mytest;

import java.io.*;
import java.net.Socket;

public class MessageThread implements Runnable{
    private Socket socket;
    public MessageThread(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        try {
            InputStream inputStream=socket.getInputStream();
            DataInputStream dataInputStream=new DataInputStream(inputStream);
            String line=null;
            while ((line=dataInputStream.readUTF())!=null){
                System.out.println(socket.getInetAddress()+"1"+line);
                sendMsg((socket.getInetAddress()+"1"+line));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void sendMsg(String msg){
        for (Socket client:UseService.socketList){
            try {
                OutputStream outputStream= client.getOutputStream();
                DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
                dataOutputStream.writeUTF(msg);
                dataOutputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
